package com.yc.rpc.core;

import com.yc.rpc.core.client.NettyClient;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Method;

public class YcRpcOutboundHandler extends ChannelOutboundHandlerAdapter implements InvocationHandler {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {


    }

    ApplicationContext context;

    public YcRpcOutboundHandler(ApplicationContext context) {
        this.context = context;
        nettyClient = context.getBean(NettyClient.class);
    }

    private NettyClient nettyClient ;


    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {


        Metadata personJsonObject = new Metadata();
        personJsonObject.setMethod(method.getName());
        personJsonObject.setClazz(method.getDeclaringClass().getName());
        personJsonObject.setParameterTypes(method.getParameterTypes());
        personJsonObject.setParameters(objects);

        nettyClient.connect("127.0.0.1", 8088);

        return null;
    }
}